package com.sgb.goods.view.adapter.publish.pop;

import android.content.Context;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsPopStorageBinding;
import com.sgb.goods.entity.StorageManEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.adapter.RecordPointCommonAdapter;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.widget.pop.GoodsPopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.List;

/**
 * 商品发布-仓储弹窗
 * @author cyj
 */
public class StoragePopWindow extends GoodsPopWindow implements SmartRefreshLayoutHelper {

    private GoodsPopStorageBinding mDataBinding;
    private InputTextInterface inputTextInterface;
    public RecordPointCommonAdapter<StorageManEntity> storageAdapter;
    public ObservableField<String> contentText = new ObservableField<>("");
    /** 是否显示添加仓储布局 */
    public ObservableBoolean hasAddStorage;

    public StoragePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        hasAddStorage = new ObservableBoolean(true);
        storageAdapter = new RecordPointCommonAdapter<StorageManEntity>(R.layout.goods_item_storage, BR.viewModel) {
            @Override
            public boolean equivalentToObject(StorageManEntity object1, StorageManEntity object2) {
                if(null == object1 || null == object2) {
                    return false;
                }
                return object1.storageName.equals(object2.storageName);
            }
        };
        return R.layout.goods_pop_storage;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        holder.setBinding(BR.spw, this);
        mDataBinding = (GoodsPopStorageBinding) holder.getmBinding();
        initListener(mDataBinding);
    }

    private void initListener(GoodsPopStorageBinding mDataBinding) {

        mDataBinding.okView.setOnClickListener(v -> {
            storageAdapter.setRecordPoint();
            if (onResultClick != null && null != this.storageAdapter.oldObj) {
                onResultClick.result(this.storageAdapter.oldObj, null, null);
            }
            dissMiss();
        });

        mDataBinding.resetView.setOnClickListener(v -> {
            storageAdapter.cancelRecordPoint();
            dismiss();
        });

        mDataBinding.llAdd.setOnClickListener(view ->
                ARouter.getInstance().build(Constants.ARouter.AR_ADD_STORAGE)
                .navigation(GoodsSdk.getInstance().getTopActivity(), 665));

        // 软键盘搜索按钮点击事件
        mDataBinding.etView.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(textView);
                inputTextInterface.input(contentText.get());
            }
            return false;
        });
    }

    public void setData(List<StorageManEntity> list, boolean isLodMore) {
        // 加载更多或者是上拉刷新数据赋值
        if(isLodMore) {
            storageAdapter.addData(list);
        } else {
            storageAdapter.setNewData(list);
        }

        // 使用组件的代理实现对上拉下拉加载数据的控制
        refreshUi(mDataBinding.rlvLayout.refreshLayout, storageAdapter, list);

        // 设置选中
        storageAdapter.setRecordPoint(storageAdapter.oldObj);
    }

    public void setOnRefreshLoadMoreListener(OnRefreshLoadMoreListener listener) {
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(listener);
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }
}
